//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.farm.doc.server.impl;

import com.farm.authority.domain.Organization;
import com.farm.authority.domain.Post;
import com.farm.authority.service.OrganizationServiceInter;
import com.farm.authority.service.UserServiceInter;
import com.farm.core.auth.domain.LoginUser;
import com.farm.core.time.TimeTool;
import com.farm.doc.dao.FarmDocDaoInter;
import com.farm.doc.dao.FarmRfDoctypeDaoInter;
import com.farm.doc.domain.Doc;
import com.farm.doc.domain.FarmDoctype;
import com.farm.doc.server.FarmDocOperateRightInter;
import com.farm.doc.server.FarmDocTypeInter;
import com.farm.doc.server.FarmDocgroupManagerInter;
import com.farm.doc.server.FarmDocOperateRightInter.POP_TYPE;
import com.farm.doc.server.plus.domain.Doctypepop;
import java.util.Iterator;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class FarmDocOperateRightImpl implements FarmDocOperateRightInter {
	@Resource
	private FarmDocDaoInter farmDocDao;
	@Resource
	private FarmDocgroupManagerInter farmdocgroupServer;
	@Resource
	private FarmDocTypeInter farmDocTypeManagerImpl;
	@Resource
	private FarmRfDoctypeDaoInter farmRfDoctypeDao;
	@Resource
	private OrganizationServiceInter organizationServiceImpl;
	@Resource
	UserServiceInter userServiceImpl;

	public FarmDocOperateRightImpl() {
	}

	@Transactional
	public boolean isDel(LoginUser user, Doc doc) {
		FarmDoctype type = this.farmRfDoctypeDao.getDocType(doc.getId());
		if (type != null && !type.getAuditpop().equals("0")) {
			return false;
		} else if (user == null) {
			return false;
		} else if (!doc.getWritepop().equals("2") && !doc.getWritepop().equals("3")
				&& doc.getCuser().equals(user.getId())) {
			return true;
		} else {
			if (doc.getWritepop().equals("2")) {
				if (doc.getDocgroupid() == null) {
					return false;
				}

				if (this.farmdocgroupServer.isAdminForGroup(user.getId(), doc.getDocgroupid())) {
					return true;
				}
			}

			return false;
		}
	}

	private boolean isRead(LoginUser user, Doc doc, FarmDoctype type) {
		if (!doc.getReadpop().equals("1")) {
			if (doc.getReadpop().equals("0")) {
				return user != null && doc.getCuser().equals(user.getId());
			} else {
				if (doc.getReadpop().equals("2")) {
					if (doc.getDocgroupid() != null && !this.farmdocgroupServer.isJoinCheck(doc.getDocgroupid())) {
						return true;
					}

					if (user != null && doc.getDocgroupid() != null
							&& this.farmdocgroupServer.isJoinGroupByUser(doc.getDocgroupid(), user.getId())) {
						return true;
					}
				}

				return doc.getReadpop().equals("3") ? false : false;
			}
		} else {
			if (type == null) {
				type = this.farmRfDoctypeDao.getDocType(doc.getId());
			}

			if (type == null) {
				return true;
			} else if (type.getReadpop().equals("0")) {
				return true;
			} else if (user == null) {
				return false;
			} else {
				Organization org = this.userServiceImpl.getUserOrganization(user.getId());
				List<Post> posts = this.userServiceImpl.getPost(user.getId());
				List<Doctypepop> pops = this.farmDocTypeManagerImpl.getTypePops(type.getId());
				Iterator var8 = pops.iterator();

				while (true) {
					Doctypepop node;
					do {
						if (!var8.hasNext()) {
							return false;
						}

						node = (Doctypepop) var8.next();
					} while (!node.getFuntype().equals("1"));

					if (node.getOid().equals(user.getId())) {
						return true;
					}

					if (org != null && org.getTreecode().indexOf(node.getOid()) >= 0) {
						return true;
					}

					Iterator var10 = posts.iterator();

					while (var10.hasNext()) {
						Post post = (Post) var10.next();
						if (node.getOid().equals(post.getId())) {
							return true;
						}
					}
				}
			}
		}
	}

	@Transactional
	public boolean isRead(LoginUser user, Doc doc) {
		return this.isRead(user, doc, (FarmDoctype) null);
	}

	@Transactional
	public boolean isAllUserRead(Doc doc) {
		return this.isRead((LoginUser) null, doc);
	}

	@Transactional
	public boolean isAllUserRead(Doc doc, FarmDoctype type) {
		return this.isRead((LoginUser) null, doc, type);
	}

	@Transactional
	public boolean isWrite(LoginUser user, Doc doc) {
		if (!doc.getWritepop().equals("1")) {
			if (doc.getWritepop().equals("0")) {
				return user != null && doc.getCuser().equals(user.getId());
			} else {
				if (doc.getWritepop().equals("2")) {
					if (doc.getDocgroupid() != null && !this.farmdocgroupServer.isJoinCheck(doc.getDocgroupid())) {
						return true;
					}

					if (user != null && doc.getDocgroupid() != null
							&& this.farmdocgroupServer.isJoinGroupByUser(doc.getDocgroupid(), user.getId())) {
						return true;
					}
				}

				return doc.getWritepop().equals("3") ? false : false;
			}
		} else {
			FarmDoctype type = this.farmRfDoctypeDao.getDocType(doc.getId());
			if (type.getWritepop().equals("0")) {
				return true;
			} else if (user == null) {
				return false;
			} else if (doc.getDomtype().equals("5")) {
				return false;
			} else {
				Organization org = this.userServiceImpl.getUserOrganization(user.getId());
				List<Post> posts = this.userServiceImpl.getPost(user.getId());
				List<Doctypepop> pops = this.farmDocTypeManagerImpl.getTypePops(type.getId());
				Iterator var8 = pops.iterator();

				while (true) {
					Doctypepop node;
					do {
						if (!var8.hasNext()) {
							return false;
						}

						node = (Doctypepop) var8.next();
					} while (!node.getFuntype().equals("2"));

					if (node.getOid().equals(user.getId())) {
						return true;
					}

					if (org != null && org.getTreecode().indexOf(node.getOid()) >= 0) {
						return true;
					}

					Iterator var10 = posts.iterator();

					while (var10.hasNext()) {
						Post post = (Post) var10.next();
						if (node.getOid().equals(post.getId())) {
							return true;
						}
					}
				}
			}
		}
	}

	@Transactional
	public Doc editDocRight(String docId, POP_TYPE pop_type_read, POP_TYPE pop_type_write, LoginUser currentUser) {
		Doc entity2 = this.farmDocDao.getEntity(docId);
		entity2.setWritepop(pop_type_write.getValue());
		entity2.setReadpop(pop_type_read.getValue());
		entity2.setEuser(currentUser.getId());
		entity2.setEusername(currentUser.getName());
		entity2.setEtime(TimeTool.getTimeDate14());
		this.farmDocDao.editEntity(entity2);
		return entity2;
	}

	@Transactional
	public void flyDoc(String id, LoginUser currentUser) {
		Doc doc = this.farmDocDao.getEntity(id);
		if (this.isDel(currentUser, doc)) {
			this.flyDoc(doc);
		} else {
			throw new RuntimeException("您没有此权限");
		}
	}

	@Transactional
	public void flyDoc(Doc doc) {
		doc.setDocgroupid((String) null);
		doc.setReadpop(POP_TYPE.PUB.getValue());
		doc.setWritepop(POP_TYPE.PUB.getValue());
		this.farmDocDao.editEntity(doc);
	}

	public FarmDocDaoInter getFarmDocDao() {
		return this.farmDocDao;
	}

	public void setFarmDocDao(FarmDocDaoInter farmDocDao) {
		this.farmDocDao = farmDocDao;
	}

	public FarmDocgroupManagerInter getFarmdocgroupServer() {
		return this.farmdocgroupServer;
	}

	public void setFarmdocgroupServer(FarmDocgroupManagerInter farmdocgroupServer) {
		this.farmdocgroupServer = farmdocgroupServer;
	}

	@Transactional
	public boolean isAudit(LoginUser user, Doc doc) {
		FarmDoctype type = this.farmRfDoctypeDao.getDocType(doc.getId());
		if (type.getAuditpop().equals("0")) {
			return false;
		} else if (user == null) {
			return false;
		} else {
			Organization org = this.userServiceImpl.getUserOrganization(user.getId());
			List<Post> posts = this.userServiceImpl.getPost(user.getId());
			List<Doctypepop> pops = this.farmDocTypeManagerImpl.getTypePops(type.getId());
			Iterator var8 = pops.iterator();

			while (true) {
				Doctypepop node;
				do {
					if (!var8.hasNext()) {
						return false;
					}

					node = (Doctypepop) var8.next();
				} while (!node.getFuntype().equals("3"));

				if (node.getOid().equals(user.getId())) {
					return true;
				}

				if (org != null && org.getTreecode().indexOf(node.getOid()) >= 0) {
					return true;
				}

				Iterator var10 = posts.iterator();

				while (var10.hasNext()) {
					Post post = (Post) var10.next();
					if (node.getOid().equals(post.getId())) {
						return true;
					}
				}
			}
		}
	}
}
